#include <stdio.h>
#include <stdlib.h>
#include "BussinessTerminalManage.h"
#include "ApiBaseInfo.h"
#include "cjson/cJSON.h"
#include "iota.h"
#include "zlog.h"
#include "common.h"
#include "security_manage.h"
#include "map.h"

extern ob_context g_mqtt_ob;
extern S_4GModule g_s_4GModule[3];
extern char* VERSION_NO;
extern int g_TerminalPublish;
extern ST_MODEL_CTL g_st_model_ctl;
extern char* AUTH_PATH;
extern ST_NETINFO g_all_netInfo;
static long g_loss_count = 0;
static int g_send_secc_count = 0;

extern char g_ServerIp[64];
extern char g_ServerPort[64];

extern int smc_tls;
extern void SendAuthToScheduler(uint8_t *psAddr);
extern map_void_t g_SubDeviceID;			
extern map_void_t g_CcoStaDeviceID;	

//用于召测线程单例实现
static pthread_mutex_t acq_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
static bool acq_thread_running = false; // 标记召测线程是否运行

static pthread_mutex_t switchSmcAuth_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
static bool switchSmcAuth_thread_running = false; //

int terminalLoss()
{
	g_loss_count++;
	return 0;
}

int terminalSend()
{
	g_send_secc_count++;
	return 0;
}

int StructToJson_TerminalManageAdd(char* ciMid, char* deviceSn, PS_TERMINAL_MANAGE psTerminalManage, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* c4GModule = NULL;
	cJSON* net = NULL;
	cJSON* infos = NULL;
	cJSON* wireless = NULL;
	cJSON* wirelessinfo = NULL;  //兼容老版本  只拿ppp0
	cJSON* wirelessinfos = NULL; //新场景：多网卡
	PS_NETCARD psNetCard = &(psTerminalManage->sNetCard);
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddStringToObject(dev, "deviceId", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "BaseInfo");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateObject());

		///
		cJSON_AddNumberToObject(data, "CPUCores", psTerminalManage->iCPUCores);
		cJSON_AddNumberToObject(data, "CPUCount", psTerminalManage->iCPUCount);
		cJSON_AddStringToObject(data, "CPUArch", psTerminalManage->cCPUArch);
		cJSON_AddStringToObject(data, "CPUFrequency", psTerminalManage->cCPUFrequency);
		cJSON_AddStringToObject(data, "CPUModel", "Cortex-A55");
		cJSON_AddStringToObject(data, "diskCapacity", psTerminalManage->cdiskCapacity);
		cJSON_AddStringToObject(data, "agentVersion", VERSION_NO);
		cJSON_AddStringToObject(data, "firmwareVersion", psTerminalManage->cfirmwareVersion);
		cJSON_AddStringToObject(data, "memCapacity", psTerminalManage->cmemCapacity);
		cJSON_AddStringToObject(data, "patchVersion", psTerminalManage->cpatchVersion);
		cJSON_AddStringToObject(data, "osKernelVer", psTerminalManage->cosKernelVer);

		cJSON_AddStringToObject(data, "osReleaseName", psTerminalManage->cosReleaseName);
		cJSON_AddStringToObject(data, "osVersion", psTerminalManage->cosVersion);
		char ipport[32] = { 0 };
		sprintf(ipport, "%s:%d", g_mqtt_ob.clientInfo.server_addr, g_mqtt_ob.clientInfo.port);
		cJSON_AddStringToObject(data, "ipPortCombo", ipport);
		dzlog_info("StructToJson_TerminalManageAdd smc_tls[%d]", smc_tls);
		cJSON_AddNumberToObject(data, "smcTls", smc_tls);
		cJSON_AddItemToObject(data, "wiredModules", infos = cJSON_CreateArray());
		int i = 0;
		for (; i < psTerminalManage->iNetCardCount; i++)
		{
			cJSON_AddItemToArray(infos, net = cJSON_CreateObject());
			cJSON_AddStringToObject(net, "iface", psNetCard->iface);
			cJSON_AddStringToObject(net, "ip", psNetCard->ip);
			cJSON_AddStringToObject(net, "mac", psNetCard->mac);
			cJSON_AddStringToObject(net, "subnetMask", psNetCard->netmask);
			cJSON_AddStringToObject(net, "gateway", psNetCard->gateway);
			psNetCard++;
		}
		//兼容老版本  只拿ppp0
		cJSON_AddItemToObject(data, "4GModule", wirelessinfo = cJSON_CreateObject());
		cJSON_AddNumberToObject(wirelessinfo, "bandWidth", psTerminalManage->s4GModule[0].bandWidth);
		cJSON_AddStringToObject(wirelessinfo, "imsi", psTerminalManage->s4GModule[0].imsi);
		cJSON_AddStringToObject(wirelessinfo, "ip", psTerminalManage->s4GModule[0].ip);
		cJSON_AddStringToObject(wirelessinfo, "manufacture", psTerminalManage->s4GModule[0].manufacture);
		cJSON_AddStringToObject(wirelessinfo, "model", psTerminalManage->s4GModule[0].model);
		cJSON_AddStringToObject(wirelessinfo, "netType", psTerminalManage->s4GModule[0].netType);
		cJSON_AddStringToObject(wirelessinfo, "simId", psTerminalManage->s4GModule[0].simId);
		cJSON_AddStringToObject(wirelessinfo, "iface", psTerminalManage->s4GModule[0].iface);
		cJSON_AddStringToObject(wirelessinfo, "carrierInformation", psTerminalManage->s4GModule[0].carrierInformation);

		//新场景：多网卡
		cJSON_AddItemToObject(data, "4GModules", wirelessinfos = cJSON_CreateArray());
		for (int j = 0; j < 2; j++)//最多2个
		{
			if (psTerminalManage->s4GModule[j].iface[0] == '\0') // 检查接口名是否有效
				continue;
			cJSON_AddItemToArray(wirelessinfos, wireless = cJSON_CreateObject());
			cJSON_AddNumberToObject(wireless, "bandWidth", psTerminalManage->s4GModule[j].bandWidth);
			cJSON_AddStringToObject(wireless, "imsi", psTerminalManage->s4GModule[j].imsi);
			cJSON_AddStringToObject(wireless, "ip", psTerminalManage->s4GModule[j].ip);
			cJSON_AddStringToObject(wireless, "manufacture", psTerminalManage->s4GModule[j].manufacture);
			cJSON_AddStringToObject(wireless, "model", psTerminalManage->s4GModule[j].model);
			cJSON_AddStringToObject(wireless, "netType", psTerminalManage->s4GModule[j].netType);
			cJSON_AddStringToObject(wireless, "simId", psTerminalManage->s4GModule[j].simId);
			cJSON_AddStringToObject(wireless, "iface", psTerminalManage->s4GModule[j].iface);
			cJSON_AddStringToObject(wireless, "carrierInformation", psTerminalManage->s4GModule[j].carrierInformation);
		}


		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int terminalManage(char* ciMid, char* deviceSn)
{
	S_NETCARD sNetCard[100];
	S_TERMINAL_MANAGE sTerminalManage = { 0 };

	// sTerminalManage.sNetCard;
	// sTerminalManage.s4GModule;
	setNetIp(g_mqtt_ob.clientInfo.server_addr); // 开机设置一次
	cpuModel(sTerminalManage.cCPUModel) && (sTerminalManage.cCPUModel[0] = '\0');
	cpuArch(sTerminalManage.cCPUArch) && (sTerminalManage.cCPUArch[0] = '\0');
	char cResult[1024] = { 0 };
	if (cpuCount(cResult) == 0)
	{
		if (cResult != NULL)
		{
			sTerminalManage.iCPUCount = atoi(cResult);
		}
	}
	!cpuCores(cResult) && (sTerminalManage.iCPUCores = atoi(cResult));
	cpuFrequency(sTerminalManage.cCPUFrequency) && (sTerminalManage.cCPUFrequency[0] = '\0');
	memCapacity(sTerminalManage.cmemCapacity) && (sTerminalManage.cmemCapacity[0] = '\0');
	char buf[128] = { 0 };
	strcpy(buf, sTerminalManage.cmemCapacity);
	diskCapacity(sTerminalManage.cdiskCapacity) && (sTerminalManage.cdiskCapacity[0] = '\0');
	// osKernelVer(sTerminalManage.cosKernelVer) && (sTerminalManage.cosKernelVer[0] = '\0');
	osVersion(sTerminalManage.cosKernelVer) && (sTerminalManage.cosKernelVer[0] = '\0');
	osReleaseName(sTerminalManage.cosReleaseName) && (sTerminalManage.cosReleaseName[0] = '\0');
	printf("osReleaseName = %s\n\n\n", sTerminalManage.cosReleaseName);
	osVersion_V1(sTerminalManage.cosVersion) && (sTerminalManage.cosVersion[0] = '\0');
	firmwareVersion(sTerminalManage.cfirmwareVersion) && (sTerminalManage.cfirmwareVersion[0] = '\0');
	int ret = wiredModules(&sTerminalManage.sNetCard);
	if (ret > 0)
	{
		int i = 0;
		for (; i < ret; ++i)
			printf("%s,%s,%s\n", sTerminalManage.sNetCard[i].iface, sTerminalManage.sNetCard[i].ip, sTerminalManage.sNetCard[i].mac);
	}
	sTerminalManage.iNetCardCount = ret;
	char* jsonb = NULL;
	if (g_st_model_ctl._4gModules == 1)
	{
		ret = _4gModules(sTerminalManage.s4GModule);
		if (ret == 0)
		{
			for (int i = 0; i < 2; i++)//最多不会超过2个无线网卡设备
			{
				// dzlog_info("interface:%d ", i);
				if (sTerminalManage.s4GModule[i].iface[0] != '\0') // 检查接口名是否有效
				{
					// dzlog_info("interface1 %s", sTerminalManage.s4GModule[i].iface);
					strcpy(g_s_4GModule[i].imsi, sTerminalManage.s4GModule[i].imsi);
					strcpy(g_s_4GModule[i].simId, sTerminalManage.s4GModule[i].simId);
					strcpy(g_s_4GModule[i].iface, sTerminalManage.s4GModule[i].iface);
				}
			}
		}
	}

	strcpy(sTerminalManage.cmemCapacity, buf);
	int ret2 = StructToJson_TerminalManageAdd(ciMid, deviceSn, &sTerminalManage, &jsonb);
	if (ret2 == 0)
	{
		printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/baseInfo", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalReboot sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}

int terminalSyncTime(char* pcJson)
{
	printf(pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* json2 = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char ntpAddress[128] = { 0 };
	int ntpPort = 0;
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	// char requestId[128] = {0};
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		// strcpy(requestId, cJSON_GetObjectItem(json, "requestId")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);

		if (cJSON_HasObjectItem(json, "params"))
		{
			params = cJSON_GetObjectItem(json, "params");
			strcpy(ntpAddress, cJSON_GetObjectItem(params, "ntpAddress")->valuestring);
			ntpPort = cJSON_GetObjectItem(params, "ntpPort")->valueint;
		}
		int ret = syncTime(msgTime, result); // syncNtp(ntpAddress, result);
		if (ret == 0)
		{
			code = 200;
		}
		else
		{
			code = 400;
		}
		cJSON_Delete(json);
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			// cJSON_AddStringToObject(json2, "requestId", requestId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);

			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("terminalSyncTime sendMsg erro ret[%d]\n", ret);
			}
			cJSON_Delete(json2);
			free(pcJsonRes);
		}
	}
	return 0;
}

int terminalReboot(char* pcJson)
{
	dzlog_info("terminalReboot start");
	printf(pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* json2 = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char reason[128] = { 0 };
	int delayTime = 0;
	int action = 0;
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	// char requestId[128] = {0};
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	dzlog_info("terminalReboot start 11");
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		dzlog_info("terminalReboot start 22");
		if (cJSON_HasObjectItem(json, "deviceId"))
		{
			strcpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		}
		else
		{
			strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		}

		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);
		dzlog_info("terminalReboot start 33");
		if (cJSON_HasObjectItem(json, "params"))
		{
			params = cJSON_GetObjectItem(json, "params");
			if (cJSON_HasObjectItem(params, "reason"))
			{
				strcpy(reason, cJSON_GetObjectItem(params, "reason")->valuestring);
			}
			if (cJSON_HasObjectItem(params, "dealyTime"))
			{
				delayTime = cJSON_GetObjectItem(params, "dealyTime")->valueint;
			}
			if (cJSON_HasObjectItem(params, "action"))
			{
				action = cJSON_GetObjectItem(params, "action")->valueint;
			}
		}
		if (action == 0)
		{
			delayTime = 0;
		}
		dzlog_info("terminalReboot start 44");
		int code[2] = { 0 };
		code[0] = 100;
		code[1] = 200;

		cJSON_Delete(json);
		int i = 0;
		for (; i < 2; ++i)
		{
			json2 = cJSON_CreateObject();
			if (json2)
			{
				char uuid[64] = { 0 };
				GF_GetGUID(uuid);
				dzlog_info("terminalReboot start 55");
				cJSON_AddStringToObject(json2, "mid", uuid);
				cJSON_AddNumberToObject(json2, "code", code[i]);
				cJSON_AddStringToObject(json2, "replyId", mid);
				cJSON_AddStringToObject(json2, "zone", zone);
				cJSON_AddStringToObject(json2, "node", node);
				cJSON_AddStringToObject(json2, "deviceId", deviceId);
				// cJSON_AddStringToObject(json2, "requestId", requestId);
				cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
				dzlog_info("terminalReboot start 66");
				char cTime[128] = { 0 };
				int ret = systemDate2(cTime);
				cJSON_AddStringToObject(json2, "msgTime", cTime);

				char* pcJsonRes = cJSON_PrintUnformatted(json2);
				dzlog_info("terminalReboot start 77");
				ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
				if (ret < 0)
					printf("terminalReboot sendMsg erro ret[%d]\n", ret);
				cJSON_Delete(json2);
				free(pcJsonRes);
			}
		}
		dzlog_info("terminalReboot start 88");
		int ret = reBoot(delayTime, result);
	}
	return 0;
}

// int StructToJson_TerminalRuntimeCommonAdd(char *ciMid, char *deviceSn, PS_DATA_REPORT_NORMAL psTerminalRuntimeComm, PS_DATA_TOP5 pTop5, char **pcJson)
int StructToJson_TerminalRuntimeCommonAdd(char* ciMid, char* deviceSn, PS_DATA_REPORT_NORMAL psTerminalRuntimeComm, PS_DATA_REPORT_TOP5 pTop5, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;

	cJSON* infos = NULL;
	cJSON* top5 = NULL;

	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "RuntimeData");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateObject());

		psTerminalRuntimeComm->min = round(psTerminalRuntimeComm->min * 100) / 100;
		psTerminalRuntimeComm->max = round(psTerminalRuntimeComm->max * 100) / 100;
		psTerminalRuntimeComm->avg = round(psTerminalRuntimeComm->avg * 100) / 100;
		psTerminalRuntimeComm->current = round(psTerminalRuntimeComm->current * 100) / 100;

		cJSON_AddNumberToObject(data, "min", psTerminalRuntimeComm->min);
		cJSON_AddNumberToObject(data, "max", psTerminalRuntimeComm->max);
		cJSON_AddNumberToObject(data, "avg", psTerminalRuntimeComm->avg);
		cJSON_AddNumberToObject(data, "current", psTerminalRuntimeComm->current);
		cJSON_AddStringToObject(data, "indicator", psTerminalRuntimeComm->indicator);
		cJSON_AddStringToObject(data, "unit", psTerminalRuntimeComm->unit);
		if (strcmp("diskUsage", psTerminalRuntimeComm->indicator) == 0)
		{
			S_DATA_REPORT_DISK_SUM disk_sum = { 0 };
			cJSON* info = NULL;
			cJSON_AddItemToObject(service, "info", info = cJSON_CreateObject());
			getDiskInfo(&disk_sum);
			cJSON_AddStringToObject(info, "total", disk_sum.total);
			cJSON_AddStringToObject(info, "used", disk_sum.used);
			cJSON_AddStringToObject(info, "available", disk_sum.available);
			cJSON_AddStringToObject(info, "free", disk_sum.free);
			cJSON_AddStringToObject(data, "partition", "");
		}
		if (strcmp("cpuUsage", psTerminalRuntimeComm->indicator) == 0)
		{
			cJSON_AddItemToObject(data, "top5", infos = cJSON_CreateArray());
			int i = 0;
			int count_cpu = 0;//记录插入的条数
			for (; i < 10 && count_cpu < 5; i++)
			{
				//过滤名字为空的数据
				if (pTop5->top[i].pName[0] == '\0') {
					//dzlog_info("cput top5 --- pName == null");
					continue;
				}
				count_cpu++;
				cJSON_AddItemToArray(infos, top5 = cJSON_CreateObject());
				cJSON_AddStringToObject(top5, "pid", pTop5->top[i].pid);
				cJSON_AddStringToObject(top5, "pName", pTop5->top[i].pName);
				cJSON_AddStringToObject(top5, "user", pTop5->top[i].user);
				cJSON_AddStringToObject(top5, "cpuUsage", pTop5->top[i].usage);
			}
		}
		if (strcmp("memUsage", psTerminalRuntimeComm->indicator) == 0)
		{
			/*
			cJSON_AddNumberToObject(data, "total", pTop5->mem.total);
			cJSON_AddNumberToObject(data, "free", pTop5->mem.free);
			cJSON_AddNumberToObject(data, "used", pTop5->mem.used);
			cJSON_AddNumberToObject(data, "available", pTop5->mem.available);
			cJSON_AddNumberToObject(data, "shared", pTop5->mem.shared);
			cJSON_AddNumberToObject(data, "buff/cache", pTop5->mem.buff_cache);
			*/
			char buf[128] = { 0 };
			sprintf(buf, "%dB", pTop5->mem.total);
			cJSON_AddStringToObject(data, "total", buf);

			sprintf(buf, "%dB", pTop5->mem.free);
			cJSON_AddStringToObject(data, "free", buf);

			sprintf(buf, "%dB", pTop5->mem.used);
			cJSON_AddStringToObject(data, "used", buf);

			sprintf(buf, "%dB", pTop5->mem.available);
			cJSON_AddStringToObject(data, "available", buf);

			sprintf(buf, "%dB", pTop5->mem.shared);
			cJSON_AddStringToObject(data, "shared", buf);

			sprintf(buf, "%dB", pTop5->mem.buff_cache);
			cJSON_AddStringToObject(data, "buff/cache", buf);

			cJSON_AddItemToObject(data, "top5", infos = cJSON_CreateArray());
			int i = 0;
			int count_mem = 0;
			for (; i < 10 && count_mem < 5; i++)
			{
				if (pTop5->top[i].pName[0] == '\0') {
					dzlog_info("mem top5 --- pName == null");
					continue;
				}
				count_mem++;

				cJSON_AddItemToArray(infos, top5 = cJSON_CreateObject());
				cJSON_AddStringToObject(top5, "pid", pTop5->top[i].pid);
				cJSON_AddStringToObject(top5, "pName", pTop5->top[i].pName);
				cJSON_AddStringToObject(top5, "user", pTop5->top[i].user);
				cJSON_AddStringToObject(top5, "memUsage", pTop5->top[i].usage);
			}
		}
		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_Terminal4g(char* ciMid, char* deviceSn, PS_DATA_REPORT_4G psTerminal4g, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;

	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "LTEModuleData");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateObject());

		///
		cJSON_AddNumberToObject(data, "rxBytes", psTerminal4g->rxBytes);
		cJSON_AddNumberToObject(data, "txBytes", psTerminal4g->txBytes);
		cJSON_AddNumberToObject(data, "rxPackets", psTerminal4g->rxPackets);
		cJSON_AddNumberToObject(data, "txPackets", psTerminal4g->txPackets);
		cJSON_AddStringToObject(data, "iface", psTerminal4g->iface);
		cJSON_AddStringToObject(data, "rsrp", psTerminal4g->rsrp);
		cJSON_AddStringToObject(data, "rsrq", psTerminal4g->rsrq);
		cJSON_AddStringToObject(data, "rssi", psTerminal4g->rssi);
		cJSON_AddStringToObject(data, "sinr", psTerminal4g->sinr);
		cJSON_AddStringToObject(data, "upSpeed", psTerminal4g->upSpeed);
		cJSON_AddStringToObject(data, "downSpeed", psTerminal4g->downSpeed);
		cJSON_AddStringToObject(data, "speed", psTerminal4g->speed);

		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int terminalcallLinkTrace(char* pcJson)
{
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* json2 = NULL;
	cJSON* body = NULL;
	cJSON* info = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char spanId[128] = { 0 };
	char parentId[128] = { 0 };
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	// char requestId[128] = {0};
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		// strcpy(requestId, cJSON_GetObjectItem(json, "requestId")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);

		if (cJSON_HasObjectItem(json, "params"))
		{
			params = cJSON_GetObjectItem(json, "params");
			strcpy(spanId, cJSON_GetObjectItem(params, "spanId")->valuestring);
			strcpy(parentId, cJSON_GetObjectItem(params, "parentId")->valuestring);
		}
		cJSON_Delete(json);
		code = 200;
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceSn);
			// cJSON_AddStringToObject(json2, "requestId", requestId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			/////////////////////////
			cJSON_AddItemToObject(json2, "body", body = cJSON_CreateArray());
			cJSON_AddItemToArray(body, info = cJSON_CreateObject());

			cJSON_AddStringToObject(info, "parentId", parentId);
			cJSON_AddStringToObject(info, "spanId", spanId);

			struct timeval tv;
			gettimeofday(&tv, NULL);
			long long milliseconds = (long long)(tv.tv_sec) * 1000 + (long long)(tv.tv_usec) / 1000;
			printf("当前毫秒级时间戳： %lld", milliseconds);
			char timetemp[64] = { 0 };
			sprintf(timetemp, "%ld", milliseconds);
			cJSON_AddStringToObject(info, "inputTime", timetemp);
			cJSON_AddStringToObject(info, "outputTime", timetemp);

			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/trace/response", pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
				printf("terminalcallLinkTrace sendMsg erro ret[%d]\n", ret);
			cJSON_Delete(json2);
			free(pcJsonRes);
		}
	}
	return 0;
}

int StructToJson_TerminalRuntimeEthernetWire(char* ciMid, char* deviceSn, PS_DATA_REPORT_WIRE psTerminalRuntimeWire, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;

	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "EthernetModuleData");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateObject());

		///
		cJSON_AddNumberToObject(data, "rxBytes", psTerminalRuntimeWire->rxBytes);
		cJSON_AddNumberToObject(data, "txBytes", psTerminalRuntimeWire->txBytes);
		cJSON_AddNumberToObject(data, "rxPackets", psTerminalRuntimeWire->rxPackets);
		cJSON_AddNumberToObject(data, "txPackets", psTerminalRuntimeWire->txPackets);
		char buf[64] = { 0 };
		sprintf(buf, "%ld", psTerminalRuntimeWire->upSpeed);
		cJSON_AddStringToObject(data, "upSpeed", buf);
		sprintf(buf, "%ld", psTerminalRuntimeWire->downSpeed);
		cJSON_AddStringToObject(data, "downSpeed", buf);
		cJSON_AddStringToObject(data, "iface", psTerminalRuntimeWire->iface);

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int getDiskInfo(S_DATA_REPORT_DISK_SUM* diskInfo)
{
	if (!diskInfo)
	{
		return -1;
	}

	char result[512] = { 0 };
	int ret;
	ret = popenCmd("df | awk '{t+=$2; u+=$3; a+=$4} END{printf \"%d %d %d\", t/1024, u/1024, a/1024}'", result);

	if (ret != 0)
		return -1;
	unsigned long total = 0, used = 0, available = 0;
	long free = 0;
	// 解析结果
	if (sscanf(result, "%lu %lu %lu", &total, &used, &available) != 3)
	{
		return -1;
	}

	//free = total - used - available; //公式有问题，导致free可能负数而类型又为unsigned long，导致数值异常
	free = available;
	sprintf(diskInfo->total, "%lu", total);
	sprintf(diskInfo->used, "%lu", used);
	sprintf(diskInfo->available, "%lu", available);
	sprintf(diskInfo->free, "%ld", free);

	return 0;
}

int StructToJson_TerminalRuntimeDataSpace(char* ciMid, char* deviceSn, PS_DATA_REPORT_DISK2 psTerminalRuntimeDisk, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* infos = NULL;
	cJSON* disk = NULL;
	cJSON* info = NULL;
	PS_DATA_REPORT_DISK pDisk = psTerminalRuntimeDisk->disk;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "DataSpace");

		cJSON_AddItemToObject(service, "data", infos = cJSON_CreateArray());
		int i = 0;
		for (; i < psTerminalRuntimeDisk->count; i++)
		{

			cJSON_AddItemToArray(infos, disk = cJSON_CreateObject());
			cJSON_AddStringToObject(disk, "available", pDisk->available);
			cJSON_AddStringToObject(disk, "availablePercent", pDisk->availablePercent);
			cJSON_AddStringToObject(disk, "fileSystem", pDisk->fileSystem);
			cJSON_AddStringToObject(disk, "mountedOn", pDisk->mountedOn);
			cJSON_AddStringToObject(disk, "size", pDisk->size);
			cJSON_AddStringToObject(disk, "used", pDisk->used);
			cJSON_AddStringToObject(disk, "usedPercent", pDisk->usedPercent);
			pDisk++;
		}
		S_DATA_REPORT_DISK_SUM disk_sum = { 0 };
		diskInfoReport2(psTerminalRuntimeDisk->disk, psTerminalRuntimeDisk->count, &disk_sum);
		cJSON_AddItemToObject(service, "info", info = cJSON_CreateObject());

		cJSON_AddStringToObject(info, "total", disk_sum.total);
		cJSON_AddStringToObject(info, "used", disk_sum.used);
		cJSON_AddStringToObject(info, "available", disk_sum.available);
		cJSON_AddStringToObject(info, "free", disk_sum.free);

		*pcJson = cJSON_PrintUnformatted(json);
		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalRuntimeCommonNetLink(char* ciMid, char* deviceSn, PS_DATA_REPORT_NETWORK psTerminalRuntimeNet, char** pcJson)
{
	// dzlog_info("ciMid[%s]",ciMid);
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* hops = NULL;
	cJSON* hop = NULL;

	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());
		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "NetLinkStatus");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateObject());

		///
		cJSON_AddStringToObject(data, "target", psTerminalRuntimeNet->target);
		cJSON_AddItemToObject(data, "hops", hops = cJSON_CreateArray());
		cJSON_AddItemToArray(hops, hop = cJSON_CreateObject());
		///
		char buf[64] = { 0 };
		cJSON_AddStringToObject(hop, "host", psTerminalRuntimeNet->host);
		sprintf(buf, "%f", psTerminalRuntimeNet->lossPct);
		cJSON_AddStringToObject(hop, "lossPct", buf);
		cJSON_AddNumberToObject(hop, "ttl", psTerminalRuntimeNet->ttl);
		cJSON_AddNumberToObject(hop, "sent", psTerminalRuntimeNet->sent);
		cJSON_AddNumberToObject(hop, "recv", psTerminalRuntimeNet->recv);
		sprintf(buf, "%f", psTerminalRuntimeNet->avg);
		cJSON_AddStringToObject(hop, "avg", buf);
		sprintf(buf, "%f", psTerminalRuntimeNet->best);
		cJSON_AddStringToObject(hop, "best", buf);
		sprintf(buf, "%f", psTerminalRuntimeNet->worst);
		cJSON_AddStringToObject(hop, "worst", buf);
		sprintf(buf, "%f", psTerminalRuntimeNet->stddev);
		cJSON_AddStringToObject(hop, "stddev", buf);

		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalprocessEvent(char* ciMid, char* deviceSn, PS_DATA_REPORT_PROCESS_EVENT psTerminalprocessEvent, int count, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* infos = NULL;
	cJSON* process = NULL;
	dzlog_info("StructToJson_TerminalprocessEvent start");
	PS_DATA_REPORT_PROCESS_EVENT pProcess = psTerminalprocessEvent;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "eventTime", cTime);
		cJSON_AddStringToObject(service, "service", "processEvent");

		cJSON_AddItemToObject(service, "data", infos = cJSON_CreateArray());
		int i = 0;
		for (; i < count; i++)
		{
			if (pProcess->process.pName[0] == '\0') {
				//dzlog_info("process  --- pName == null");
				pProcess++;
				continue;
			}

			//dzlog_info("process- pName:%s,cmd:%s",pProcess->process.pName,pProcess->process.cmd);
			cJSON_AddItemToArray(infos, process = cJSON_CreateObject());
			cJSON_AddStringToObject(process, "eventType", pProcess->eventType);

			cJSON_AddNumberToObject(process, "isFull", pProcess->isFull);
			// cJSON_AddStringToObject(process, "pid", pProcess->process.pid);
			cJSON_AddNumberToObject(process, "pid", atoi(pProcess->process.pid));
			cJSON_AddStringToObject(process, "pName", pProcess->process.pName);
			cJSON_AddStringToObject(process, "cmd", pProcess->process.cmd);
			cJSON_AddStringToObject(process, "state", pProcess->process.state);
			char cTime[128] = { 0 };
			int ret = systemDate(cTime);
			cJSON_AddStringToObject(process, "started", pProcess->process.started);
			cJSON_AddStringToObject(process, "uid", pProcess->process.uid);
			cJSON_AddStringToObject(process, "user", pProcess->process.user);
			cJSON_AddStringToObject(process, "gid", pProcess->process.gid);
			cJSON_AddStringToObject(process, "group", pProcess->process.group);
			cJSON_AddStringToObject(process, "ppid", pProcess->process.ppid);
			pProcess->process.memUsage[strlen(pProcess->process.memUsage)] = '\0';
			pProcess->process.cpuUsage[strlen(pProcess->process.cpuUsage)] = '\0';
			cJSON_AddNumberToObject(process, "cpuUsage", atof(pProcess->process.cpuUsage));
			// cJSON_AddStringToObject(process, "memUsage", pProcess->process.memUsage);

			cJSON_AddNumberToObject(process, "memUsage", atof(pProcess->process.memUsage));
			cJSON_AddStringToObject(process, "procMd5", pProcess->process.procMd5);
			pProcess++;
		}

		*pcJson = cJSON_PrintUnformatted(json);
		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalperformanceEvent(char* ciMid, char* deviceSn, PS_DATA_REPORT_PROCESS2 psTerminalformanceEvent, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* infos = NULL;
	cJSON* process = NULL;

	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "eventTime", cTime);
		cJSON_AddStringToObject(service, "service", "performanceEvent");

		cJSON_AddItemToObject(service, "data", infos = cJSON_CreateObject());

		cJSON_AddNumberToObject(infos, "pid", atoi(psTerminalformanceEvent->pid));

		// psTerminalformanceEvent->cpuUsage[strlen(psTerminalformanceEvent->cpuUsage) - 1] = '\0';
		printf("psTerminalformanceEvent->cpuUsage[%s]\n", psTerminalformanceEvent->cpuUsage);
		cJSON_AddNumberToObject(infos, "cpuUsage", atof(psTerminalformanceEvent->cpuUsage));
		printf("psTerminalformanceEvent->memUsage[%s]\n", psTerminalformanceEvent->memUsage);
		// psTerminalformanceEvent->memUsage[strlen(psTerminalformanceEvent->memUsage) - 1] = '\0';
		cJSON_AddNumberToObject(infos, "memUsage", atof(psTerminalformanceEvent->memUsage));

		cJSON_AddNumberToObject(infos, "diskUsage", atof(psTerminalformanceEvent->diskUsage));
		cJSON_AddNumberToObject(infos, "delayTime", g_all_netInfo.last);
		cJSON_AddNumberToObject(infos, "lossUsage", (float)g_loss_count / (g_loss_count + g_send_secc_count));

		///
		{
			*pcJson = cJSON_PrintUnformatted(json);
			// printf("%s\n", cJSON_PrintUnformatted(json));
			// strcpy(pcJson, cJSON_PrintUnformatted(json));
		}

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}
int StructToJson_TerminalperformanceErrorEvent(char* ciMid, char* deviceSn, char* psTerminalformanceEvent, char** pcJson, int flag)
{
	if (psTerminalformanceEvent == NULL)
	{
		return -1;
	}
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* infos = NULL;
	cJSON* process = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());
		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "eventTime", cTime);
		cJSON_AddStringToObject(service, "service", "performanceErrorEvent");

		cJSON_AddItemToObject(service, "data", infos = cJSON_CreateObject());

		cJSON_AddStringToObject(infos, "time", cTime);
		if (flag == 1)
		{
			cJSON_AddStringToObject(infos, "type", "coredump");
			cJSON_AddStringToObject(infos, "code", "coredump");
			cJSON_AddStringToObject(infos, "name", "coredump");
			cJSON_AddStringToObject(infos, "context", "0");
		}
		else
		{
			cJSON_AddStringToObject(infos, "type", "error");
			cJSON_AddStringToObject(infos, "code", "error");

			char buf[128] = { 0 };
			char buf2[128] = { 0 };
			strcpy(buf2, psTerminalformanceEvent);
			if (strcmp(psTerminalformanceEvent, "R") == 0)
			{
				strcpy(buf2, "Runnable");
				strcpy(buf, "1001");
			}
			else if (strcmp(psTerminalformanceEvent, "S") == 0)
			{
				strcpy(buf2, "Sleeping");
				strcpy(buf, "1002");
			}
			else if (strcmp(psTerminalformanceEvent, "K") == 0)
			{
				strcpy(buf2, "Stopped");
				strcpy(buf, "1003");
			}
			else if (strcmp(psTerminalformanceEvent, "I") == 0)
			{
				strcpy(buf2, "Zombie");
				strcpy(buf, "1004");
			}
			else
			{
				strcpy(buf2, "Zombie");
				strcpy(buf, "1004");
			}
			cJSON_AddStringToObject(infos, "name", buf2);
			cJSON_AddStringToObject(infos, "context", buf);
		}

		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalserviceEvent(char* ciMid, char* deviceSn, PS_DATA_REPORT_SERVER_EVENT psTerminalserviceEvent, int count, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* infos = NULL;
	cJSON* serviceEvent = NULL;
	PS_DATA_REPORT_SERVER_EVENT pService = psTerminalserviceEvent;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "eventTime", cTime);
		cJSON_AddStringToObject(service, "service", "serviceEvent");

		cJSON_AddItemToObject(service, "data", infos = cJSON_CreateArray());
		int i = 0;
		for (; i < count; i++)
		{

			cJSON_AddItemToArray(infos, serviceEvent = cJSON_CreateObject());
			cJSON_AddStringToObject(serviceEvent, "eventType", pService->eventType);

			cJSON_AddNumberToObject(serviceEvent, "isFull", pService->isFull);
			cJSON_AddStringToObject(serviceEvent, "path", pService->server.path);
			cJSON_AddStringToObject(serviceEvent, "name", pService->server.name);
			cJSON_AddStringToObject(serviceEvent, "installedTime", pService->server.installedTime);
			cJSON_AddStringToObject(serviceEvent, "updatedTime", pService->server.updatedTime);
			cJSON_AddStringToObject(serviceEvent, "isAutoStarted", pService->server.isAutoStarted);
			cJSON_AddStringToObject(serviceEvent, "status", pService->server.status);

			cJSON_AddStringToObject(serviceEvent, "pid", pService->server.pid);
			cJSON_AddStringToObject(serviceEvent, "description", pService->server.description);
			cJSON_AddStringToObject(serviceEvent, "prococolName", pService->server.procotolName);
			cJSON_AddStringToObject(serviceEvent, "protocolType", pService->server.protocolType);
			cJSON_AddStringToObject(serviceEvent, "protocolVersion", pService->server.protocolVersion);
			pService++;
		}

		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalnetstatEvent(char* ciMid, char* deviceSn, PS_DATA_REPORT_PORT_EVENT psTerminalnetstatEvent, int count, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* infos = NULL;
	cJSON* netstatEvent = NULL;
	PS_DATA_REPORT_PORT_EVENT pPort = psTerminalnetstatEvent;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "eventTime", cTime);
		cJSON_AddStringToObject(service, "service", "netstatEvent");

		cJSON_AddItemToObject(service, "data", infos = cJSON_CreateArray());
		int i = 0;
		for (; i < count; i++)
		{

			cJSON_AddItemToArray(infos, netstatEvent = cJSON_CreateObject());
			cJSON_AddStringToObject(netstatEvent, "eventType", pPort->eventType);

			cJSON_AddNumberToObject(netstatEvent, "isFull", pPort->isFull);
			cJSON_AddStringToObject(netstatEvent, "localIp", pPort->port.localIp);
			// cJSON_AddStringToObject(netstatEvent, "localPort", pPort->port.localPort);
			cJSON_AddNumberToObject(netstatEvent, "localPort", atoi(pPort->port.localPort));
			cJSON_AddStringToObject(netstatEvent, "foreignIp", pPort->port.foreignIp);
			cJSON_AddStringToObject(netstatEvent, "foreignPort", pPort->port.foreignPort);
			cJSON_AddStringToObject(netstatEvent, "protocol", pPort->port.protocol);
			cJSON_AddStringToObject(netstatEvent, "state", pPort->port.state);

			cJSON_AddNumberToObject(netstatEvent, "pid", pPort->port.pid);
			cJSON_AddStringToObject(netstatEvent, "pName", pPort->port.pName);
			pPort++;
		}

		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalKeepAlive(char* ciMid, char* deviceSn, PS_DATA_REPORT_ALIVE psTerminalAlive, int count, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;

	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate3(cTime);
		cJSON_AddStringToObject(service, "eventTime", cTime);
		cJSON_AddStringToObject(service, "service", "KeepAlive");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateObject());
		cJSON_AddStringToObject(data, "eventType", "KeepAlive");

		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalRuntimeContainer(char* ciMid, char* deviceSn, PS_DATA_REPORT_CONTAINER_EVENT psTerminalRuntimeContainer, int count, char** pcJson)
{
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* datatemp = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());
		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());
		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "ContainerRuntimeData");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateArray());
		///
		int i = 0;
		for (; i < count; i++)
		{
			cJSON_AddItemToArray(data, datatemp = cJSON_CreateObject());
			cJSON_AddStringToObject(datatemp, "containerId", psTerminalRuntimeContainer->containerId);
			cJSON_AddStringToObject(datatemp, "name", psTerminalRuntimeContainer->name);
			// cJSON_AddStringToObject(datatemp, "version", psTerminalRuntimeContainer->version);
			cJSON_AddStringToObject(datatemp, "type", psTerminalRuntimeContainer->type);
			// cJSON_AddStringToObject(datatemp, "manufacture", psTerminalRuntimeContainer->manufacture);
			cJSON_AddStringToObject(datatemp, "manufacture", "ZNYW");
			cJSON_AddStringToObject(datatemp, "status", psTerminalRuntimeContainer->status);
			cJSON_AddStringToObject(datatemp, "deployTime", psTerminalRuntimeContainer->deployTime);
			cJSON_AddNumberToObject(datatemp, "cpuUsed", psTerminalRuntimeContainer->cpuUsed);
			cJSON_AddNumberToObject(datatemp, "memUsed", psTerminalRuntimeContainer->memUsed);
			cJSON_AddNumberToObject(datatemp, "diskUsed", psTerminalRuntimeContainer->diskUsed);
			psTerminalRuntimeContainer++;
		}
		{
			*pcJson = cJSON_PrintUnformatted(json);
		}
		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int StructToJson_TerminalRuntimeApp(char* ciMid, char* deviceSn, PS_DATA_REPORT_APP_EVENT psTerminalRuntimeApp, int count, char** pcJson)
{
	// dzlog_info("ciMid[%s]",ciMid);
	cJSON* json = NULL;
	cJSON* service = NULL;
	cJSON* services = NULL;
	cJSON* dev = NULL;
	cJSON* devs = NULL;
	cJSON* data = NULL;
	cJSON* hops = NULL;
	cJSON* hop = NULL;
	cJSON* datatemp = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceSn", deviceSn);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());
		cJSON_AddStringToObject(service, "mid", ciMid);
		char cTime[128] = { 0 };
		int ret = systemDate(cTime);
		cJSON_AddStringToObject(service, "dataTime", cTime);
		cJSON_AddStringToObject(service, "service", "AppRuntimeData");
		cJSON_AddItemToObject(service, "data", data = cJSON_CreateArray());
		///
		int i = 0;
		for (; i < count; i++)
		{
			cJSON_AddItemToArray(data, datatemp = cJSON_CreateObject());
			cJSON_AddStringToObject(datatemp, "appId", psTerminalRuntimeApp->appId);
			cJSON_AddStringToObject(datatemp, "name", psTerminalRuntimeApp->name);
			cJSON_AddStringToObject(datatemp, "version", psTerminalRuntimeApp->version);
			cJSON_AddStringToObject(datatemp, "type", psTerminalRuntimeApp->type);
			// cJSON_AddStringToObject(datatemp, "manufacture", psTerminalRuntimeApp->manufacture);
			cJSON_AddStringToObject(datatemp, "manufacture", "ZNYW");
			cJSON_AddStringToObject(datatemp, "status", psTerminalRuntimeApp->status);
			cJSON_AddStringToObject(datatemp, "deployTime", psTerminalRuntimeApp->deployTime);
			// cJSON_AddStringToObject(datatemp, "deployTime", cTime);
			cJSON_AddNumberToObject(datatemp, "cpuUsed", psTerminalRuntimeApp->cpuUsed);
			cJSON_AddNumberToObject(datatemp, "memUsed", psTerminalRuntimeApp->memUsed);
			cJSON_AddNumberToObject(datatemp, "diskUsed", psTerminalRuntimeApp->diskUsed);
			cJSON_AddNumberToObject(datatemp, "isTrustworthy", psTerminalRuntimeApp->isTrustworthy);
			psTerminalRuntimeApp++;
		}
		{
			*pcJson = cJSON_PrintUnformatted(json);
			// printf("%s\n", cJSON_PrintUnformatted(json));
			// strcpy(pcJson, cJSON_PrintUnformatted(json));
		}

		cJSON_Delete(json);
		return 0;
	}
	else
	{
		return -1;
	}
}

int getSampleFrequency(char* indicator)
{
	return 0;
}
int getReportFrequency(char* indicator)
{
	return 0;
}

int terminalcpuTemp(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_NORMAL sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("cpuTemp");
	sCmd.reportFrequency = getReportFrequency("cpuTemp");
	strcpy(sCmd.indicator, "cpuTemp");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalRuntimeCommonAdd(ciMid, deviceSn, &sObj, NULL, &jsonb);
	if (ret2 == 0)
	{
		// printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/performance", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalcpuTemp sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}
int terminalcpuUsage(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_TOP5 sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("cpuUsage");
	sCmd.reportFrequency = getReportFrequency("cpuUsage");
	strcpy(sCmd.indicator, "cpuUsage");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		printf("%d\n", ret);
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalRuntimeCommonAdd(ciMid, deviceSn, &sObj.normal, &sObj, &jsonb);
	if (ret2 == 0)
	{
		// printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/performance", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalcpuUsage sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}
int terminalmemUsage(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_TOP5 sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("memUsage");
	sCmd.reportFrequency = getReportFrequency("memUsage");
	strcpy(sCmd.indicator, "memUsage");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		printf("%d\n", ret);
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalRuntimeCommonAdd(ciMid, deviceSn, &sObj.normal, &sObj, &jsonb);
	if (ret2 == 0)
	{
		// printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/performance", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalmemUsage sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}
int terminaldiskUsage(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_NORMAL sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("diskUsage");
	sCmd.reportFrequency = getReportFrequency("diskUsage");
	strcpy(sCmd.indicator, "diskUsage");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalRuntimeCommonAdd(ciMid, deviceSn, &sObj, NULL, &jsonb);
	if (ret2 == 0)
	{
		// printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/performance", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminaldiskUsage sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}

int terminalLTEModule(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_4G sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("LTEModule");

	sCmd.reportFrequency = getReportFrequency("LTEModule");
	strcpy(sCmd.indicator, "LTEModule");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_Terminal4g(ciMid, deviceSn, &sObj, &jsonb);
	if (ret2 == 0)
	{
		printf("%s\n", jsonb);
	}

	ret = IOTA_SendMqtt("/v1/devices/device/network", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalLTEModule sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}

int terminaletherModule(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	// printf("terminaletherModule !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_WIRE sObj[100] = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("etherModule");
	sCmd.reportFrequency = getReportFrequency("etherModule");
	strcpy(sCmd.indicator, "etherModule");
	int ret = dataReport(&sCmd, sObj, 0);
	if (ret <= 0)
	{
		printf("etherModule ret [%d]\n", ret);
		return -1;
	}
	char* jsonb = NULL;
	int i = 0;
	PS_DATA_REPORT_WIRE p = sObj;

	for (i = 0; i < ret; ++i)
	{
		printf("------------------------------------------terminaletherModule ret[%d] i[%d]\n", ret, i);
		int ret2 = StructToJson_TerminalRuntimeEthernetWire(ciMid, deviceSn, p++, &jsonb);
		if (ret2 == 0)
		{
			printf("%s\n", jsonb);
		}
		// printf("terminaletherModule jsonb[%s]\n", jsonb);
		ret2 = IOTA_SendMqtt("/v1/devices/device/network", jsonb, strlen(jsonb));
		if (ret2 < 0)
			printf("terminaletherModule sendMsg erro ret[%d]\n", ret2);
		free(jsonb);
		jsonb = NULL;
	}
	return 0;
}
int terminalDataSpace(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_DISK2 sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("DataSpace");
	sCmd.reportFrequency = getReportFrequency("DataSpace");
	strcpy(sCmd.indicator, "DataSpace");
	int ret = dataReport(&sCmd, sObj.disk, 0);
	if (ret <= 0)
	{
		return -1;
	}
	char* jsonb = NULL;
	sObj.count = ret;

	int ret2 = StructToJson_TerminalRuntimeDataSpace(ciMid, deviceSn, &sObj, &jsonb);
	if (ret2 == 0)
	{
		printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/performance", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalDataSpace sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}
int terminalNetLinkStatus(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	// char midBuf[128] = {0};
	// strcpy(midBuf,ciMid);
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_NETWORK sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("NetLinkStatus");
	sCmd.reportFrequency = getReportFrequency("NetLinkStatus");
	strcpy(sCmd.indicator, "NetLinkStatus");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalRuntimeCommonNetLink(ciMid, deviceSn, &sObj, &jsonb);
	if (ret2 == 0)
	{
		printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/network", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalNetLinkStatus sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}
int terminalprocessEvent(char* ciMid, char* deviceSn, int now) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	PS_DATA_REPORT_PROCESS_EVENT sObj = (PS_DATA_REPORT_PROCESS_EVENT)malloc(sizeof(S_DATA_REPORT_PROCESS_EVENT) * 330);
	memset(sObj, 0, sizeof(S_DATA_REPORT_PROCESS_EVENT) * 330);
	sCmd.sampleFrequency = getSampleFrequency("processEvent");
	sCmd.reportFrequency = getReportFrequency("processEvent");
	strcpy(sCmd.indicator, "processEvent");
	int ret = dataReport(&sCmd, sObj, now);
	if (ret <= 0)
	{
		free(sObj);
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalprocessEvent(ciMid, deviceSn, sObj, ret, &jsonb);
	if (ret2 == 0)
	{
		// printf("terminalprocessEvent %s\n", jsonb);
		dzlog_info("terminalprocessEvent ret[%d]", ret);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));
	if (ret < 0)
	{
		printf("terminalprocessEvent sendMsg erro ret[%d]\n", ret);
		dzlog_error("terminalprocessEvent sendMsg erro ret[%d]", ret);
	}
	free(sObj);
	free(jsonb);
	return 0;
}

int terminalperformanceEvent(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	// PS_DATA_REPORT_PROCESS_EVENT sObj[200] = {0};
	S_DATA_REPORT_PROCESS2 sObj = { 0 };

	sCmd.sampleFrequency = getSampleFrequency("performanceEvent");
	sCmd.reportFrequency = getReportFrequency("performanceEvent");
	strcpy(sCmd.indicator, "performanceEvent");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		printf("performanceEvent ret %d\n", ret);
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalperformanceEvent(ciMid, deviceSn, &sObj, &jsonb);
	if (ret2 == 0)
	{
		// printf("performanceEvent %s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));
	if (ret < 0)
	{
		printf("performanceEvent sendMsg erro ret[%d]\n", ret);
		dzlog_error("performanceEvent sendMsg erro ret[%d]", ret);
	}

	free(jsonb);
	return 0;
}
int terminalperformanceErrorEvent(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	// PS_DATA_REPORT_PROCESS_EVENT sObj[200] = {0};
	char sObj[128] = { 0 };

	sCmd.sampleFrequency = getSampleFrequency("performanceErrorEvent");
	sCmd.reportFrequency = getReportFrequency("performanceErrorEvent");
	strcpy(sCmd.indicator, "performanceErrorEvent");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		printf("performanceErrorEvent ret %d\n", ret);
		return -1;
	}
	printf("performanceErrorEvent sObj %s\n", sObj);

	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalperformanceErrorEvent(ciMid, deviceSn, sObj, &jsonb, 0);
	if (ret2 == 0)
	{
		dzlog_info("performanceErrorEvent ret[%d]", ret);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));
	if (ret < 0)
	{
		dzlog_error("performanceErrorEvent sendMsg erro ret[%d]", ret);
	}

	free(jsonb);
	return 0;
}

int terminalperformanceCoreDumpEvent(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	/*
		S_DATA_REPORT_CMD sCmd = {0};
		// PS_DATA_REPORT_PROCESS_EVENT sObj[200] = {0};
		char sObj[128] = {0};

		sCmd.sampleFrequency = getSampleFrequency("performanceErrorEvent");
		sCmd.reportFrequency = getReportFrequency("performanceErrorEvent");
		strcpy(sCmd.indicator, "performanceErrorEvent");
		int ret = dataReport(&sCmd, &sObj);
		if (ret <= 0)
		{
			printf("performanceErrorEvent ret %d\n", ret);
			dzlog_info("performanceErrorEvent ret[%d]", ret);
			return -1;
		}*/
	char* jsonb = NULL;
	time_t time;
	char buf[40] = { 0 };
	char event[40] = { 0 };
	char stime[40] = { 0 };
	if (calculate_file_size("/tmp/EagleEyeSMG.pid") > 0)
	{
		FILE* fp = fopen("/tmp/EagleEyeSMG.pid", "r");
		if (fp == NULL)
		{
			dzlog_error("open file error");
			return -1;
		}
		fgets(buf, sizeof(buf), fp);
		char* p = strtok(buf, "|");
		if (p != NULL)
		{
			strcpy(event, p);
		}
		p = strtok(NULL, "|");
		if (p != NULL)
		{
			strcpy(stime, p);
			time = atoi(stime);
		}
		fclose(fp);
	}
	dzlog_info("terminalperformanceCoreDumpEvent event[%s] time[%ld]", event, time);
	int ret2 = -1;

	if (strcmp(event, "stop") == 0)
	{
		ret2 = StructToJson_TerminalperformanceErrorEvent(ciMid, deviceSn, "K", &jsonb, 0);
	}
	else if (strcmp(event, "codedump") == 0)
	{
		ret2 = StructToJson_TerminalperformanceErrorEvent(ciMid, deviceSn, "E", &jsonb, 1);
	}
	else
	{
		ret2 = StructToJson_TerminalperformanceErrorEvent(ciMid, deviceSn, NULL, &jsonb, 0);
	}

	if (ret2 == 0)
	{
		printf("performanceErrorEvent %s\n", jsonb);
		dzlog_info("performanceErrorEvent ret[%d]", ret2);
	}
	else if (ret2 == -1)
	{
		return 0;
	}
	int ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));

	if (ret < 0)
	{
		printf("performanceErrorEvent sendMsg erro ret[%d]\n", ret);
		dzlog_error("performanceErrorEvent sendMsg erro ret[%d]", ret);
	}
	system("rm -f /tmp/EagleEyeSMG.pid");
	free(jsonb);
	return 0;
}
int terminalserviceEvent(char* ciMid, char* deviceSn, int now) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };

	PS_DATA_REPORT_SERVER_EVENT sObj = (PS_DATA_REPORT_SERVER_EVENT)malloc(sizeof(S_DATA_REPORT_SERVER_EVENT) * 250);
	if (sObj == NULL)
	{
		return -1;
	}
	memset(sObj, 0, sizeof(S_DATA_REPORT_SERVER_EVENT) * 250);

	sCmd.sampleFrequency = getSampleFrequency("serviceEvent");
	sCmd.reportFrequency = getReportFrequency("serviceEvent");
	strcpy(sCmd.indicator, "serviceEvent");
	printf("terminalserviceEvent +++++++++++++++++++++++\n");
	int ret = dataReport(&sCmd, sObj, now);
	printf("terminalserviceEvent  ret[%d]", ret);
	if (ret <= 0)
	{
		free_s(sObj);
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalserviceEvent(ciMid, deviceSn, sObj, ret, &jsonb);
	if (ret2 == 0)
	{
		// printf("%s\n", jsonb);
	}
	printf("terminalserviceEvent ----------------------------\n");
	ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));

	if (ret < 0)
		printf("terminalserviceEvent sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	free_s(sObj);
	return 0;
}
int terminalnetstatEvent(char* ciMid, char* deviceSn, int now) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	// S_DATA_REPORT_PORT_EVENT sObj[50] = {0};
	PS_DATA_REPORT_PORT_EVENT sObj = (PS_DATA_REPORT_PORT_EVENT)malloc(sizeof(S_DATA_REPORT_PORT_EVENT) * 200);
	memset(sObj, 0, sizeof(S_DATA_REPORT_PORT_EVENT) * 200);
	sCmd.sampleFrequency = getSampleFrequency("netstatEvent");
	sCmd.reportFrequency = getReportFrequency("netstatEvent");
	strcpy(sCmd.indicator, "netstatEvent");
	int ret = dataReport(&sCmd, sObj, now);
	if (ret <= 0)
	{
		free(sObj);
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalnetstatEvent(ciMid, deviceSn, sObj, ret, &jsonb);
	if (ret2 == 0)
	{
		// printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalnetstatEvent sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	free(sObj);
	return 0;
}

int terminalKeepAlive(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	S_DATA_REPORT_CMD sCmd = { 0 };
	PS_DATA_REPORT_ALIVE sObj = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("KeepAlive");
	sCmd.reportFrequency = getReportFrequency("KeepAlive");
	strcpy(sCmd.indicator, "KeepAlive");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalKeepAlive(ciMid, deviceSn, &sObj, ret, &jsonb);
	if (ret2 == 0)
	{
		printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/heartbeat", jsonb, strlen(jsonb));
	// ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalnetstatEvent sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}

int terminalManageBaseInfo() // 系统基本信息上报，上报频率：程序启动后上报一次
{
	char uuid[64] = { 0 };
	GF_GetGUID(uuid);
	dzlog_info("terminalManage start");
	int ret = terminalManage(uuid, g_mqtt_ob.clientInfo.device_id);
	dzlog_info("terminalManage end");

	GF_GetGUID(uuid);
	dzlog_info("terminalperformanceCoreDumpEvent start+++++++++++++++++++++++++++++++++++++++++++++++");
	ret = terminalperformanceCoreDumpEvent(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	dzlog_info("terminalperformanceCoreDumpEvent end-----------------------------------------------");

	return ret;
}

int terminalContainerRuntimeData(char* ciMid, char* deviceSn) // 系统运行数据上报
{
	// char midBuf[128] = {0};
	// strcpy(midBuf,ciMid);
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_CONTAINER_EVENT sObj[50] = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("ContainerRuntimeData");
	sCmd.reportFrequency = getReportFrequency("ContainerRuntimeData");
	strcpy(sCmd.indicator, "ContainerRuntimeData");
	int ret = dataReport(&sCmd, &sObj, 0);
	if (ret <= 0)
	{
		return -1;
	}

	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalRuntimeContainer(ciMid, deviceSn, &sObj, ret, &jsonb);
	if (ret2 == 0)
	{
		// dzlog_info("terminalContainerRuntimeData 2223  jsonb[%s]", jsonb);
	}
	sleep(2);
	ret = IOTA_SendMqtt("/v1/devices/device/datas", jsonb, strlen(jsonb));
	if (ret < 0)
	{
		printf("terminalNetLinkStatus sendMsg erro ret[%d]\n", ret);
	}
	free(jsonb);
	return 0;
}

int terminalAppRuntimeData(char* ciMid, char* deviceSn, int now) // 系统运行数据上报
{
	// char midBuf[128] = {0};
	// strcpy(midBuf,ciMid);
	S_DATA_REPORT_CMD sCmd = { 0 };
	S_DATA_REPORT_APP_EVENT sObj[50] = { 0 };
	sCmd.sampleFrequency = getSampleFrequency("AppRuntimeData");
	sCmd.reportFrequency = getReportFrequency("AppRuntimeData");
	strcpy(sCmd.indicator, "AppRuntimeData");
	int ret = dataReport(&sCmd, &sObj, now);
	if (ret < 0)
	{
		return -1;
	}
	char* jsonb = NULL;
	int ret2 = StructToJson_TerminalRuntimeApp(ciMid, deviceSn, &sObj, ret, &jsonb);
	if (ret2 == 0)
	{
		printf("%s\n", jsonb);
	}
	ret = IOTA_SendMqtt("/v1/devices/device/datas", jsonb, strlen(jsonb));
	if (ret < 0)
		printf("terminalNetLinkStatus sendMsg erro ret[%d]\n", ret);
	free(jsonb);
	return 0;
}

int terminalRuntimeData() // 系统运行数据上报
{
	g_TerminalPublish = 1;
	char uuid[64] = { 0 };
	GF_GetGUID(uuid);
	int ret = terminalcpuTemp(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	GF_GetGUID(uuid);
	ret = terminalcpuUsage(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	GF_GetGUID(uuid);
	ret = terminalmemUsage(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	GF_GetGUID(uuid);
	// dzlog_info("terminaldiskUsage start");
	ret = terminaldiskUsage(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	if (g_st_model_ctl._4gModules == 1)
	{
		GF_GetGUID(uuid);
		ret = terminalLTEModule(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	}

	GF_GetGUID(uuid);
	// dzlog_info("terminaletherModule start");
	ret = terminaletherModule(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	// dzlog_info("terminaletherModule end");
	GF_GetGUID(uuid);
	// dzlog_info("terminalDataSpace start");
	ret = terminalDataSpace(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	// dzlog_info("terminalDataSpace end");
	GF_GetGUID(uuid);
	// dzlog_info("terminalNetLinkStatus start");
	ret = terminalNetLinkStatus(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	// dzlog_info("terminalNetLinkStatus end");

	GF_GetGUID(uuid);
	// dzlog_info("terminalprocessEvent start");
	ret = terminalprocessEvent(uuid, g_mqtt_ob.clientInfo.device_id, 0); // 系统运行数据上报
	// dzlog_info("terminalprocessEvent end");

	GF_GetGUID(uuid);
	// dzlog_info("terminalperformanceEvent start+++++++++++++++++++++++++++++++++++++++++++++++");
	ret = terminalperformanceEvent(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	// dzlog_info("terminalperformanceEvent end-----------------------------------------------");

	GF_GetGUID(uuid);
	// dzlog_info("terminalperformanceErrorEvent start+++++++++++++++++++++++++++++++++++++++++++++++");
	ret = terminalperformanceErrorEvent(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报

	GF_GetGUID(uuid);
	// dzlog_info("terminalnetstatEvent start");
	ret = terminalnetstatEvent(uuid, g_mqtt_ob.clientInfo.device_id, 0); // 系统运行数据上报
	// dzlog_info("terminalnetstatEvent end");
	GF_GetGUID(uuid);
	// dzlog_info("KeepAlive start");
	ret = terminalKeepAlive(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	GF_GetGUID(uuid);
	ret = terminalContainerRuntimeData(uuid, g_mqtt_ob.clientInfo.device_id); // 系统运行数据上报
	if (g_st_model_ctl.appModules == 1)
	{
		GF_GetGUID(uuid);
		// dzlog_info("terminalAppRuntimeData start");
		ret = terminalAppRuntimeData(uuid, g_mqtt_ob.clientInfo.device_id, 0); // 系统运行数据上报
																			   // dzlog_info("terminalAppRuntimeData end");
	}
	g_TerminalPublish = 0;
	return 0;
}

int terminalSetDataReportConfig(char* pcJson)
{
	printf(pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* info = NULL;
	cJSON* json2 = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char indicator[128] = { 0 };
	int sampleFrequency = 0;
	int reportFrequency = 0;
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	// char requestId[128] = {0};
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		if (cJSON_HasObjectItem(json, "zone"))
		{
			strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		}
		if (cJSON_HasObjectItem(json, "node"))
		{
			strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		}
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);
		S_DATA_REPORT_CMD sCmd = { 0 };
		int ret = 0;
		///
		if (cJSON_HasObjectItem(json, "params"))
		{
			params = cJSON_GetObjectItem(json, "params");
			int i = 0;
			for (i = 0; i < cJSON_GetArraySize(params); i++)
			{
				info = cJSON_GetArrayItem(params, i);
				strcpy(sCmd.indicator, cJSON_GetObjectItem(info, "indicator")->valuestring);
				sCmd.sampleFrequency = cJSON_GetObjectItem(info, "sampleFrequency")->valueint;
				sCmd.reportFrequency = cJSON_GetObjectItem(info, "reportFrequency")->valueint;
				ret = updateCmd(&sCmd);
			}
		}
		///
		/*
		if (cJSON_HasObjectItem(json, "params"))
		{
			params = cJSON_GetObjectItem(json, "params");
			strcpy(cmd.indicator, cJSON_GetObjectItem(params, "indicator")->valuestring);
			cmd.sampleFrequency = cJSON_GetObjectItem(params, "sampleFrequency")->valueint;
			cmd.reportFrequency = cJSON_GetObjectItem(params, "reportFrequency")->valueint;
		}
		*/

		if (ret == 0)
		{
			code = 200;
		}
		else
		{
			code = 400;
		}
		cJSON_Delete(json);
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			// cJSON_AddStringToObject(json2, "requestId", requestId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);

			char* jsonb = NULL;
			{

				// strcpy(pcJsonRes, cJSON_PrintUnformatted(json2));
				// printf("---------res\n");
				// printf(pcJsonRes);
				jsonb = cJSON_PrintUnformatted(json2);
			}
			ret = IOTA_SendMqtt("/v1/devices/device/response", jsonb, strlen(jsonb));
			if (ret < 0)
				printf("terminalReboot sendMsg erro ret[%d]\n", ret);
			cJSON_Delete(json2);
			free(jsonb);
		}
	}

	return 0;
}

int setFullReport_pthread(char* pJson)
{
	cJSON* json = NULL;
	cJSON* params = NULL;

	json = cJSON_Parse(pJson);
	if (json)
	{
		char uuid[64] = { 0 };
		GF_GetGUID(uuid);
		if (cJSON_HasObjectItem(json, "params"))
		{
			params = cJSON_GetObjectItem(json, "params");
			int i = 0;
			for (i = 0; i < cJSON_GetArraySize(params); i++)
			{
				char buf[32] = { 0 };
				strcpy(buf, cJSON_GetArrayItem(params, i)->valuestring);
				printf("%s\n", buf);

				if (strcmp("process", buf) == 0)
				{
					PS_DATA_REPORT_PROCESS_EVENT sObj = (PS_DATA_REPORT_PROCESS_EVENT)malloc(sizeof(S_DATA_REPORT_PROCESS_EVENT) * 330);
					memset(sObj, 0, sizeof(S_DATA_REPORT_PROCESS_EVENT) * 330);
					int ret = fullInfoReport(buf, sObj);
					if (ret <= 0)
					{
						free(sObj);
						free_s(pJson);
						return -1;
					}
					char* jsonb = NULL;
					int ret2 = StructToJson_TerminalprocessEvent(uuid, g_mqtt_ob.clientInfo.device_id, sObj, ret, &jsonb);
					if (ret2 == 0)
					{
						// printf("%s\n", jsonb);
					}
					ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));
					if (ret < 0)
						printf("terminalprocessEvent sendMsg erro ret[%d]\n", ret);
					free(jsonb);
					free(sObj);
				}
				else if (strcmp("service", buf) == 0)
				{

				}
				else if (strcmp("netstat", buf) == 0)
				{
					PS_DATA_REPORT_PORT_EVENT sObj = (PS_DATA_REPORT_PORT_EVENT)malloc(sizeof(S_DATA_REPORT_PORT_EVENT) * 200);
					memset(sObj, 0, sizeof(S_DATA_REPORT_PORT_EVENT) * 200);

					int ret = fullInfoReport(buf, sObj);
					dzlog_info("ret[%d]", ret);
					if (ret <= 0)
					{
						free(sObj);
						free_s(pJson);
						return -1;
					}
					char* jsonb = NULL;
					int ret2 = StructToJson_TerminalnetstatEvent(uuid, g_mqtt_ob.clientInfo.device_id, sObj, ret, &jsonb);
					if (ret2 == 0)
					{
						// printf("%s\n", jsonb);
					}
					ret = IOTA_SendMqtt("/v1/devices/device/events", jsonb, strlen(jsonb));
					if (ret < 0)
						printf("terminalnetstatEvent sendMsg erro ret[%d]\n", ret);
					free(jsonb);
					free(sObj);
				}
			}
		}
		cJSON_Delete(json);
	}
	free_s(pJson);
	return 0;
}

int terminalprocessCmd(char* pcJson)
{
	//return 0;

	printf("%s\n", pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* param = NULL;
	cJSON* json2 = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char reason[128] = { 0 };
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	// char requestId[128] = {0};
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		if (cJSON_HasObjectItem(json, "zone"))
		{
			strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		}
		if (cJSON_HasObjectItem(json, "node"))
		{
			strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		}
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);

		code = 200;//后端此接口是同步接口，全量上报太久，所以先在此处直接响应200
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			if (zone != NULL && strlen(zone) != 0)
			{
				cJSON_AddStringToObject(json2, "zone", zone);
			}
			if (node != NULL && strlen(node) != 0)
			{
				cJSON_AddStringToObject(json2, "node", node);
			}
			cJSON_AddStringToObject(json2, "deviceId", deviceSn);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);

			char* jsonb = NULL;
			{
				jsonb = cJSON_PrintUnformatted(json2);
			}
			ret = IOTA_SendMqtt("/v1/devices/device/response", jsonb, strlen(jsonb));
			if (ret < 0)
				printf("processCmd sendMsg erro ret[%d]\n", ret);
			cJSON_Delete(json2);
			free(jsonb);
		}

		//由于实际业务执行耗时较长，且会影响setFullReport本身的响应，所以起线程执行业务
		// 分线程
		pthread_t thread;
		pthread_attr_t attr;
		char* psJson = strdup(pcJson);
		// 初始化线程属性
		pthread_attr_init(&attr);
		// 将线程设置为分离状态
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	
		// 创建线程
		pthread_create(&thread, &attr, setFullReport_pthread, psJson);
		// 销毁线程属性对象
		pthread_attr_destroy(&attr);


		cJSON_Delete(json);

	}
	return 0;
}

int terminaldataSpaceService(char* pcJson)
{
	printf(pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* json2 = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char reason[128] = { 0 };

	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	// char requestId[128] = {0};
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	// int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		// strcpy(requestId, cJSON_GetObjectItem(json, "requestId")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);

		if (cJSON_HasObjectItem(json, "params"))
		{
		}
		cJSON_Delete(json);

		///
		S_DATA_REPORT_DISK2 sObj = { 0 };
		setDiskReportTime();

		int ret = diskInfoReport(sObj.disk);
		if (ret <= 0)
		{
			return -1;
		}
		char* jsonb = NULL;
		sObj.count = ret;

		int ret2 = StructToJson_TerminalRuntimeDataSpace(mid, deviceSn, &sObj, &jsonb);
		if (ret2 == 0)
		{
			printf("%s\n", jsonb);
		}
		ret = IOTA_SendMqtt("/v1/devices/device/performance", jsonb, strlen(jsonb));
		if (ret < 0)
			printf("terminalDataSpace sendMsg erro ret[%d]\n", ret);
		free(jsonb);
		///

		int code[2] = { 0 };
		code[0] = 100;
		code[1] = 200;

		int i = 0;
		for (; i < 2; ++i)
		{
			json2 = cJSON_CreateObject();
			if (json2)
			{
				char uuid[64] = { 0 };
				GF_GetGUID(uuid);
				cJSON_AddStringToObject(json2, "mid", uuid);
				cJSON_AddNumberToObject(json2, "code", code[i]);
				cJSON_AddStringToObject(json2, "replyId", mid);
				cJSON_AddStringToObject(json2, "zone", zone);
				cJSON_AddStringToObject(json2, "node", node);
				cJSON_AddStringToObject(json2, "deviceId", deviceId);
				// cJSON_AddStringToObject(json2, "requestId", requestId);
				cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
				char cTime[128] = { 0 };
				int ret = systemDate2(cTime);
				cJSON_AddStringToObject(json2, "msgTime", cTime);

				char* jsonb = NULL;
				{

					// strcpy(pcJsonRes, cJSON_PrintUnformatted(json2));
					// printf("---------res\n");
					// printf(pcJsonRes);
					jsonb = cJSON_PrintUnformatted(json2);
				}
				ret = IOTA_SendMqtt("/v1/devices/device/response", jsonb, strlen(jsonb));
				if (ret < 0)
					printf("terminalReboot sendMsg erro ret[%d]\n", ret);
				cJSON_Delete(json2);
				free(jsonb);
			}
		}
	}
	return 0;
}

int nvrConfiguration(char* pcJson)
{
	dzlog_info("nvrConfiguration start 00");
	cJSON* json = NULL;
	cJSON* json2 = NULL;
	cJSON* body = NULL;
	int ret = -1;
	char mid[64] = { 0 };
	char filePath[254] = { 0 };
	char zone[8] = { 0 };
	char node[8] = { 0 };
	char deviceSn[64] = { 0 };
	char deviceId[64] = { 0 };
	// char requestId[254] = { 0 };
	char resMsg[128] = { 0 };
	int code = -1;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);

		//判断mid是否已经存在
		ret = SqlVLCFGPUSHLOGCOUNTBYMID(mid);

		strcpy(filePath, cJSON_GetObjectItem(json, "filePath")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		body = cJSON_GetObjectItem(json, "body");
		dzlog_info("nvrConfiguration mid repetition ret[%d]", ret);
		if (ret > 0)
		{
			code = 200;
		}
		else
		{
			ret = toSetnvrConfig(body->valuestring, filePath, resMsg);
			code = 200;
			//查询记录数，如果多余50，就删除
			SqlDeleteVLCFGPUSHLOG();

			time_t rawtime;
			struct tm* timeinfo;
			char buffer[80];
			time(&rawtime);
			timeinfo = localtime(&rawtime);
			// 格式化为标准时间格式
			strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
			//插记录表，mid code 报文
			SqlINSERTIntoVLCFGPUSHLOG(mid, buffer, 200, body->valuestring);
			//
		}
		cJSON_Delete(json);
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("nvrConfiguration sendMsg erro ret[%d]\n", ret);
			}
			free(pcJsonRes);
			cJSON_Delete(json2);
		}
	}
	return 0;
}

int retrieveModuleLogDir(char* pcJson)
{
	dzlog_info("retrieveModuleLogDir start 00");
	cJSON* json = NULL;
	cJSON* json2 = NULL;
	cJSON* body = NULL;
	cJSON* info = NULL;
	int ret = -1;
	char mid[64] = { 0 };
	char zone[8] = { 0 };
	char node[8] = { 0 };
	char deviceSn[64] = { 0 };
	char deviceId[64] = { 0 };
	// char requestId[254] = { 0 };
	char resMsg[128] = { 0 };
	int code = -1;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		cJSON_Delete(json);

		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			code = 100;
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			cJSON_AddStringToObject(json2, "message", "Data acquisition in progress");
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("nvrConfiguration sendMsg erro ret[%d]\n", ret);
			}
			free(pcJsonRes);

			cJSON_ReplaceItemInObject(json2, "message", cJSON_CreateString("success"));
			cJSON_ReplaceItemInObject(json2, "code", cJSON_CreateNumber(200));
			cJSON_AddItemToObject(json2, "body", body = cJSON_CreateArray());
			cJSON_AddItemToArray(body, info = cJSON_CreateObject());
			cJSON_AddStringToObject(info, "smg_log_path", "/PowerHarmony/SMC/app/EagleEyeSMG/log/");

			cJSON_AddStringToObject(info, "app_log_path", "/");

			cJSON_AddStringToObject(info, "sys_log_path", "/var/log/");

			pcJsonRes = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("nvrConfiguration sendMsg erro ret[%d]\n", ret);
			}
			free(pcJsonRes);
			cJSON_Delete(json2);
		}
	}
	return 0;
}

int romaConfiguration(char* pcJson)
{
	dzlog_info("romaConfiguration start 00");
	cJSON* json = NULL;
	cJSON* json2 = NULL;
	cJSON* body = NULL;
	int ret = -1;
	char mid[64] = { 0 };
	char zone[8] = { 0 };
	char node[8] = { 0 };
	char deviceSn[64] = { 0 };
	char deviceId[64] = { 0 };
	char resMsg[128] = { 0 };
	char* connectserver = NULL;
	char* connectreginfo = NULL;
	int code = -1;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		body = cJSON_GetObjectItem(json, "params");
		if (body)
		{
			connectserver = cJSON_PrintUnformatted(cJSON_GetObjectItem(body, "connectserver"));
			connectreginfo = cJSON_PrintUnformatted(cJSON_GetObjectItem(body, "connectreginfo"));
		}
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			code = 100;
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[64] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("romaConfiguration sendMsg erro ret[%d]\n", ret);
			}
			free(pcJsonRes);
			cJSON_Delete(json2);
		}
		cJSON* json1 = NULL;
		json1 = cJSON_Parse(connectreginfo);
		if (json1)
		{
			if (cJSON_HasObjectItem(json1, "TLS"))
			{
				int iot_tls_tmp = cJSON_GetObjectItemCaseSensitive(json1, "TLS")->valueint;
				set_iot_info_v1(iot_tls_tmp);
				if (iot_tls_tmp)
				{
					//获取roma证书
					//downRomaCert();
				}
			}
			cJSON_Delete(json1);
		}

		ret = toSetRomaConfig(connectserver, connectreginfo, resMsg);

		cJSON_Delete(json);
		if (ret >= 0)
		{
			code = 200;
		}
		else
		{
			code = 400;
		}

		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);

			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("romaConfiguration sendMsg erro ret[%d]\n", ret);
			}
			free(pcJsonRes);
			cJSON_Delete(json2);
		}

		if (connectserver != NULL)
		{
			free_s(connectserver);
			free_s(connectreginfo);
		}
	}
	return 0;
}

int terminalManageConf(char* pcJson)
{
	dzlog_info("terminalManageConf start 00");
	cJSON* json = NULL;
	char cmd[32] = { 0 };
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strncpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring, sizeof(cmd));
		dzlog_info("terminalManageConf cmd[%s] ", cmd);
		// 下发规则配置
		if (strstr(cmd, "nvrConfiguration")) // NVR配置（主子码相关）
		{
			nvrConfiguration(pcJson);
		}
		else if (strstr(cmd, "retrieveModuleLogDir"))
		{
			retrieveModuleLogDir(pcJson);
		}
		else if (strstr(cmd, "romaConfiguration"))
		{
			romaConfiguration(pcJson);
		}

		cJSON_Delete(json);
	}
	return 0;
}

int ThirdModuleMain(char* pcJson)
{
	cJSON* json = NULL;
	char deviceId[64] = { 0 };
	char event[32] = { 0 };
	char cmd[32] = { 0 };
	json = cJSON_Parse(pcJson);
	if (json)
	{

		cJSON* tmp = cJSON_GetObjectItem(json, "cmd");
		if (tmp)
		{
			strncpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring, sizeof(cmd));
		}

		tmp = cJSON_GetObjectItem(json, "event");
		if (tmp)
		{
			strncpy(event, cJSON_GetObjectItem(json, "event")->valuestring, sizeof(event));
		}

		tmp = cJSON_GetObjectItem(json, "deviceId");
		if (tmp)
		{
			strncpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring, sizeof(deviceId));
		}


		printf("==================================\n");
		printf("ThirdModuleMain cmd[%s] \n", cmd);
		printf("ThirdModuleMain event[%s] \n", event);
		printf("ThirdModuleMain deviceId[%s] \n", deviceId);
		printf("ThirdModuleMain pcJson[%s] \n", pcJson);
		printf("==================================\n");


		cJSON_Delete(json);
	}
	return 0;
}

int PlcModuleMain(char* pcJson)
{
	cJSON* json = NULL;
	cJSON* json2 = NULL;
	char deviceId[64] = { 0 };
	char deviceSn[64] = { 0 };
	char service[32] = { 0 };
	char cmd[32] = { 0 };
	char tmpjson[512] = { 0 };
	char mid[32] = { 0 };
	char zone[32] = { 0 };
	char node[32] = { 0 };
	char requestId[32] = { 0 };
	int code = 100;
	
	char topic[64] = { 0 };
	json = cJSON_Parse(pcJson);
	if (json)
	{

		cJSON* tmp = cJSON_GetObjectItem(json, "cmd");
		if (tmp)
		{
			strncpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring, sizeof(cmd));
		}

		tmp = cJSON_GetObjectItem(json, "service");
		if (tmp)
		{
			strncpy(service, cJSON_GetObjectItem(json, "service")->valuestring, sizeof(service));
		}

		tmp = cJSON_GetObjectItem(json, "deviceId");
		if (tmp)
		{
			strncpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring, sizeof(deviceId));
		}

		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		//strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		//strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		//strcpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		//strcpy(requestId, cJSON_GetObjectItem(json, "requestId")->valuestring);
		cJSON_Delete(json);

		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };

			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			//cJSON_AddStringToObject(json2, "zone", zone);
			//cJSON_AddStringToObject(json2, "node", node);
			//cJSON_AddStringToObject(json2, "deviceId", deviceId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			//cJSON_AddStringToObject(json2, "requestId", requestId);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);

			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			sprintf(topic, "/v1/devices/%s/plc/response", deviceSn);
			ret = IOTA_SendMqtt(topic, pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("PlcModuleMain sendMsg code 100 erro ret[%d]\n", ret);
			}
			free(pcJsonRes);

		}

		cJSON_Delete(json2);
	}
	return 0;
}

int eagleEyeInit(char* pcJson)
{
	cJSON* json = NULL;
	cJSON* params = NULL;

	/// req
	char mid[64] = { 0 };
	char deviceSn[64] = { 0 };

	int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);

		// 上报baseInfo
		terminalManageBaseInfo();

		//// 设置配置开关
		// char path1[256] = { 0 };
		// sprintf(path1, "sed -i \"s/enable = 1/enable = 0/\" %s", AUTH_PATH);
		// system(path1);
		// sprintf(path1, "sed -i \"s/enable =1/enable = 0/\" %s", AUTH_PATH);
		// system(path1);
		// 启动拉取物联配置线程（线程内是拉取到才结束）
		//ActivationMain();

		cJSON_Delete(json);
	}
	return 0;
}

int terminalUsbPlugging(char* pcJson)
{
	printf(pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* json2 = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char ntpAddress[128] = { 0 };
	int ntpPort = 0;
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	char portNumber[32] = { 0 };
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);
		if (cJSON_HasObjectItem(json, "params"))
		{
			params = cJSON_GetObjectItem(json, "params");
			strcpy(portNumber, cJSON_GetObjectItem(params, "portNumber")->valuestring);
		}
		char type[3] = { 0 };
		strncpy(type, "0", sizeof(type) - 1);
		if (strcmp("usbOpen", cmd) == 0)
		{
			strncpy(type, "1", sizeof(type) - 1);
		}

		int ret = setAuthorized(type, result, portNumber); // syncNtp(ntpAddress, result);
		if (ret == 0 && strcmp(type, result) == 0)
		{
			code = 200;
		}
		else
		{
			code = 400;
		}
		cJSON_Delete(json);
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			if (pcJsonRes)
			{
				ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
				if (ret < 0)
				{
					printf("terminalUsbPlugging sendMsg erro ret[%d]\n", ret);
				}
				free(pcJsonRes);
			}
			cJSON_Delete(json2);
		}
	}
	return 0;
}

int terminalStartDepend(char* pcJson)
{
	printf(pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* json2 = NULL;
	cJSON* body = NULL;

	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	char msgTime[128] = { 0 };

	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);

		cJSON* tmp = cJSON_GetObjectItem(json, "zone");
		if (tmp)
		{

			strcpy(zone, tmp->valuestring);
		}
		tmp = cJSON_GetObjectItem(json, "node");
		if (tmp)
		{

			strcpy(node, tmp->valuestring);
		}

		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);

		cJSON_Delete(json);
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", 200);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", g_mqtt_ob.clientInfo.device_id);

			cJSON_AddItemToObject(json2, "body", body = cJSON_CreateObject());


			cJSON_AddStringToObject(body, "deviceSn", g_mqtt_ob.clientInfo.device_id);

			cJSON_AddStringToObject(body, "cpuId", "");

			char activat[256] = { 0 };
			cJSON_AddStringToObject(body, "activation", activat);

			char cTime[128] = { 0 };
			systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			if (pcJsonRes)
			{
				int ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
				if (ret < 0)
				{
					printf("terminalStartDepend sendMsg erro ret[%d]\n", ret);
				}
				free(pcJsonRes);
			}
			cJSON_Delete(json2);
		}
	}
	return 0;
}

int terminalUpdateCer(char* pcJson)
{
	printf(pcJson);
	cJSON* json = NULL;
	cJSON* params = NULL;
	cJSON* json2 = NULL;
	char result[128] = { 0 };
	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char ntpAddress[128] = { 0 };
	int ntpPort = 0;
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char deviceId[128] = { 0 };
	char portNumber[32] = { 0 };
	char msgTime[128] = { 0 };
	/// res
	char replyId[128] = { 0 };
	int code = 0;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);
		int ret = 0;
		if (ret == 0)
		{
			code = 200;
		}
		else
		{
			code = 400;
		}
		cJSON_Delete(json);
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceId", deviceId);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			if (pcJsonRes)
			{
				ret = IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
				if (ret < 0)
				{
					printf("terminalSyncTime sendMsg erro ret[%d]\n", ret);
				}
				free(pcJsonRes);
			}
			cJSON_Delete(json2);
		}
	}

	return 0;
}

void* run_loop_thread(void* param)
{
	pthread_detach(pthread_self());
	dzlog_info("run_loop_thread start");
	run_loop();
	dzlog_info("run_loop_thread end");

	return 0;
}

// 终端将License的内容上告平台校验
int writeDevinceSn(char* pSn)
{
	char toolResult[128] = { 0 };
	char toolCmd[32] = { 0 };

	sprintf(toolCmd, "devinfo -w NodeId=\"%s\"", pSn);
	int ret = popenCmd(toolCmd, toolResult);
	if (strlen(toolResult) > 1)
	{
		(toolResult[strlen(toolResult) - 1] == '\n') && (toolResult[strlen(toolResult) - 1] = '\0');
	}

	printf("writeDevinceSn red[%s] ", toolResult);

	if (strstr(toolResult, "suc") == NULL)
	{
		return -1;
	}

	return 0;
}

int get_seventh_line_str(const char* input)
{
	if (!input)
		return NULL;

	FILE* pipe;
	char buffer[2046];
	char* last_line = NULL;
	size_t len = 0;

	// 执行命令并捕获stdout和stderr
	pipe = popen("license_tool gene_veri_info 2>&1", "r");
	if (!pipe)
	{
		perror("popen() failed");
		return -1;
	}

	// 逐行读取输出，保存最后一行
	while (fgets(buffer, sizeof(buffer), pipe) != NULL)
	{

		printf("gene_veri_info[%s]\n", buffer);
		// 去除行尾换行符（可选）
		buffer[strcspn(buffer, "\n")] = '\0';
		// 动态分配内存保存最后一行
		free(last_line);
		last_line = strdup(buffer);
		if (!last_line)
		{
			perror("strdup() failed");
			pclose(pipe);
			return -1;
		}
	}

	// 关闭管道并检查退出状态
	int status = pclose(pipe);
	if (status == -1)
	{
		perror("pclose() failed");
		free(last_line);
		return -1;
	}
	else if (WEXITSTATUS(status) != 0)
	{
		fprintf(stderr, "Command failed with exit code %d\n", WEXITSTATUS(status));
	}

	// 输出结果
	if (last_line)
	{
		printf("Last line: %s\n", last_line);
		strcpy(input, last_line);
		free(last_line);
		return WEXITSTATUS(status);
	}
	else
	{
		printf("No output captured.\n");
	}

	return 0;
}

// 终端将License的内容上告平台校验
int geteqAuthInf(char** pResults)
{
	char toolResult[2048] = { 0 };
	//	int ret = popenCmd("license_tool gene_veri_info", toolResult);
	//    printf("===[%d] [%s]===\n\n", strlen(toolResult), toolResult);

	int ret = get_seventh_line_str(toolResult);
	printf("----[%d][%s]----\n", ret, toolResult);

	if (ret == 0)
	{
		strcpy(pResults, toolResult);
	}

	return 0;
}

//实时监控数据上报
int realTimeMonitoring(char* pcJson)
{
	dzlog_info("realTimeMonitoring start 00");
	cJSON* json = NULL;
	char cmd[32] = { 0 };
	char mid[64] = { 0 };
	char zone[16] = { 0 };
	char node[16] = { 0 };
	char deviceSn[64] = { 0 };

	json = cJSON_Parse(pcJson);
	if (json)
	{
		strncpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring, sizeof(cmd));
		strncpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring, sizeof(mid));
		strncpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring, sizeof(zone));
		strncpy(node, cJSON_GetObjectItem(json, "node")->valuestring, sizeof(node));
		strncpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring, sizeof(deviceSn));

		cJSON* json2 = NULL;
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", 100);
			cJSON_AddStringToObject(json2, "replyId", mid);
			if (zone != NULL && strlen(zone) != 0)
			{
				cJSON_AddStringToObject(json2, "zone", zone);
			}
			if (node != NULL && strlen(node) != 0)
			{
				cJSON_AddStringToObject(json2, "node", node);
			}
			cJSON_AddStringToObject(json2, "deviceId", deviceSn);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* jsonb = NULL;
			jsonb = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", jsonb, strlen(jsonb));
			cJSON_Delete(json2);
			free(jsonb);
		}

		dzlog_info("realTimeMonitoring cmd[%s] ", cmd);
		// 下发规则配置
		if (strstr(cmd, "startMonitoring")) // 开启实时监控
		{
			run_loop_v1();
		}

		json2 = NULL;
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", 200);
			cJSON_AddStringToObject(json2, "replyId", mid);
			if (zone != NULL && strlen(zone) != 0)
			{
				cJSON_AddStringToObject(json2, "zone", zone);
			}
			if (node != NULL && strlen(node) != 0)
			{
				cJSON_AddStringToObject(json2, "node", node);
			}

			cJSON_AddStringToObject(json2, "deviceId", deviceSn);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);

			char* jsonb = NULL;
			jsonb = cJSON_PrintUnformatted(json2);
			ret = IOTA_SendMqtt("/v1/devices/device/response", jsonb, strlen(jsonb));
			if (ret < 0)
				printf("processCmd sendMsg erro ret[%d]\n", ret);
			cJSON_Delete(json2);
			free(jsonb);
		}

		cJSON_Delete(json);
	}
	return 0;
}

//子设备增强认证
int terminalSubDeviceAuth(char *pcJson)
{
	cJSON* json = NULL;
	cJSON* json2 = NULL;

	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char ntpAddress[128] = { 0 };
	int ntpPort = 0;
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[32] = { 0 };
	char subDeviceSn[32] = { 0 };
	char deviceId[128] = { 0 };
	char portNumber[32] = { 0 };
	char msgTime[128] = { 0 };

	/// res
	char replyId[128] = { 0 };
	int code = 200;

	//消息解析及处理
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		cJSON* pZone = cJSON_GetObjectItem(json, "zone");
		if (pZone != NULL)
			strcpy(zone, pZone->valuestring);
		cJSON* pNode = cJSON_GetObjectItem(json, "node");
		if (pNode != NULL)
			strcpy(node, pNode->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);

		cJSON* pSubDeviceSn = cJSON_GetObjectItem(json, "subDeviceSn");
		if (pSubDeviceSn != NULL)
			strcpy(subDeviceSn, pSubDeviceSn->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);
		cJSON_Delete(json);

		if (strlen(subDeviceSn) <= 0)
			code = 400;

		if (code == 200)
		{
			map_get(&g_SubDeviceID, subDeviceSn);
			map_get(&g_CcoStaDeviceID, subDeviceSn);
			if (g_SubDeviceID.ref != NULL)						//发送开关子设备增强认证消息到MCCB
			{
				cJSON *pReqJson = NULL;
				char *psJson = NULL;
				pReqJson = cJSON_CreateObject();
				if (pReqJson != NULL)
				{
					cJSON_AddStringToObject(pReqJson, "token", mid);
					cJSON_AddStringToObject(pReqJson, "deviceSn", subDeviceSn);
					char cTime[128] = {0};
					int ret = systemDate(cTime);
					cJSON_AddStringToObject(pReqJson, "time", cTime);
			
					psJson = cJSON_PrintUnformatted(pReqJson);
					cJSON_Delete(pReqJson);
			
					if(psJson != NULL)
					{
						//dzlog_info("req:%s\n", psJson);
						IOTA_sendToLocalMqtt("/ext.syy.mccb/ext.syy.phSmc/S-smclink/F-SubDeviceAuthorize", psJson);
						free(psJson);
					}
				}
				else
					dzlog_error("cJSON_CreateObject failed!");
			}
			else if (g_CcoStaDeviceID.ref != NULL)			//发送CCO或STA增强认证消息到调度
			{
				uint8_t *pData = *(g_CcoStaDeviceID.ref);
				SendAuthToScheduler(pData);
			}
			else 
			{
				dzlog_error("can't find ESN(%s)!",subDeviceSn);
			}
	    }

		//构造响应消息
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "zone", zone);
			cJSON_AddStringToObject(json2, "node", node);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			cJSON_AddStringToObject(json2, "subDeviceSn", subDeviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			dzlog_info("req:%s\n", pcJsonRes);
			if (pcJsonRes)
			{
				IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
				free(pcJsonRes);
			}
			cJSON_Delete(json2);
		}
		else
			dzlog_error("cJSON_CreateObject failed!");
	}
	else
		dzlog_error("cJSON_Parse failed!");

	printf("terminalSubDeviceAuth finish(dev:%s,subdev:%s)\n", deviceSn, subDeviceSn);
	dzlog_info("terminalSubDeviceAuth finish(dev:%s,subdev:%s)\n", deviceSn, subDeviceSn);
	return 0;
}

int update_app_status_pthread(char* pJson)
{
	sleep(1);
	char uuid[64] = { 0 };
	GF_GetGUID(uuid);
	dzlog_info("update_app_status_pthread start");
	int ret = terminalAppRuntimeData(uuid, g_mqtt_ob.clientInfo.device_id, 1); // 系统运行数据上报
	dzlog_info("update_app_status_pthread end");

	//GF_GetGUID(uuid);
	//dzlog_info("terminalprocessEvent start");
	//ret = terminalprocessEvent(uuid, g_mqtt_ob.clientInfo.device_id, 1); // 系统运行数据上报
	//dzlog_info("terminalprocessEvent end");
	//pthread_mutex_lock(&acq_thread_mutex);
	//acq_thread_running = false;
	//pthread_mutex_unlock(&acq_thread_mutex);

	return 0;
}

int update_process_status_pthread(char* pJson)
{
	sleep(1);
	char uuid[64] = { 0 };
	GF_GetGUID(uuid);
	dzlog_info("terminalprocessEvent start");
	int ret = terminalprocessEvent(uuid, g_mqtt_ob.clientInfo.device_id, 1); // 系统运行数据上报
	dzlog_info("terminalprocessEvent end");
	return 0;
}

int update_service_status_pthread(char* pJson)
{
	sleep(1);
	char uuid[64] = { 0 };
	GF_GetGUID(uuid);
	dzlog_info("terminalserviceEvent start");
	int ret = terminalserviceEvent(uuid, g_mqtt_ob.clientInfo.device_id, 1); // 系统运行数据上报
	dzlog_info("terminalserviceEvent end");
	return 0;
}

int check_and_report_iot_changes()
{
	dzlog_info("check_and_report_iot_changes start");

	const char* filename = "./conf/iotInfo.json";
	// 开始监控文件，每2秒检查一次
	monitor_file(filename, 5);
	return 0;
}

int terminalQueryMqttScram(char* pcJson)
{
	cJSON* json = NULL;
	cJSON* json1 = NULL;

	/// req
	char mid[128] = { 0 };
	char service[128] = { 0 };
	char cmd[128] = { 0 };
	char zone[128] = { 0 };
	char node[128] = { 0 };
	char deviceSn[128] = { 0 };
	char msgTime[128] = { 0 };

	/// res
	char replyId[128] = { 0 };
	int code = 200;

	//消息解析及处理
	json = cJSON_Parse(pcJson);
	if (json)
	{
		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		strcpy(service, cJSON_GetObjectItem(json, "service")->valuestring);
		strcpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		cJSON* pZone = cJSON_GetObjectItem(json, "zone");
		if (pZone != NULL)
			strcpy(zone, pZone->valuestring);
		cJSON* pNode = cJSON_GetObjectItem(json, "node");
		if (pNode != NULL)
			strcpy(node, pNode->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		strcpy(msgTime, cJSON_GetObjectItem(json, "msgTime")->valuestring);
		cJSON_Delete(json);

		//构造响应消息
		json1 = cJSON_CreateObject();
		if (json1)
		{
			char uuid[64] = { 0 };
			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json1, "mid", uuid);
			cJSON_AddNumberToObject(json1, "code", code);
			cJSON_AddStringToObject(json1, "replyId", mid);
			cJSON_AddStringToObject(json1, "zone", zone);
			cJSON_AddStringToObject(json1, "node", node);
			cJSON_AddStringToObject(json1, "deviceSn", deviceSn);
			cJSON_AddNumberToObject(json1, "isEnableScram", g_mqtt_ob.clientInfo.scram);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json1, "msgTime", cTime);
			char* pcJsonRes = cJSON_PrintUnformatted(json1);
			dzlog_info("req:%s\n", pcJsonRes);
			if (pcJsonRes)
			{
				IOTA_SendMqtt("/v1/devices/device/response", pcJsonRes, strlen(pcJsonRes));
				free(pcJsonRes);
			}
			cJSON_Delete(json1);
		}
		else
			dzlog_error("cJSON_CreateObject failed!");
	}
	else
		dzlog_error("cJSON_Parse failed!");

	printf("terminalQueryMqttScram finish(scram:%d)\n", g_mqtt_ob.clientInfo.scram);
	dzlog_info("terminalQueryMqttScram finish(scram:%d)", g_mqtt_ob.clientInfo.scram);
	return 0;
}

int lowPowerSleepConf(char* pcJson)
{
	cJSON* json = NULL;
	cJSON* json2 = NULL;
	cJSON* body = NULL;
	char deviceId[64] = { 0 };
	char deviceSn[64] = { 0 };
	char service[32] = { 0 };
	char cmd[32] = { 0 };
	char tmpjson[512] = { 0 };
	char mid[32] = { 0 };
	char zone[32] = { 0 };
	char node[32] = { 0 };
	char requestId[32] = { 0 };
	int code = 100;

	char topic[64] = { 0 };
	json = cJSON_Parse(pcJson);
	if (json)
	{

		cJSON* tmp = cJSON_GetObjectItem(json, "cmd");
		if (tmp)
		{
			strncpy(cmd, cJSON_GetObjectItem(json, "cmd")->valuestring, sizeof(cmd));
		}

		tmp = cJSON_GetObjectItem(json, "service");
		if (tmp)
		{
			strncpy(service, cJSON_GetObjectItem(json, "service")->valuestring, sizeof(service));
		}

		tmp = cJSON_GetObjectItem(json, "deviceId");
		if (tmp)
		{
			strncpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring, sizeof(deviceId));
		}

		strcpy(mid, cJSON_GetObjectItem(json, "mid")->valuestring);
		//strcpy(zone, cJSON_GetObjectItem(json, "zone")->valuestring);
		//strcpy(node, cJSON_GetObjectItem(json, "node")->valuestring);
		strcpy(deviceSn, cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		//strcpy(deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		//strcpy(requestId, cJSON_GetObjectItem(json, "requestId")->valuestring);
		body = cJSON_GetObjectItem(json, "params");
		char* pcJsonbody = cJSON_PrintUnformatted(body);
		cJSON_Delete(json);

		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };

			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			//cJSON_AddStringToObject(json2, "requestId", requestId);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);

			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			sprintf(topic, "/v1/devices/device/response");
			ret = IOTA_SendMqtt(topic, pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("lowPowerSleepConf sendMsg code 100 erro ret[%d]\n", ret);
			}
			free(pcJsonRes);

		}
		cJSON_Delete(json2);
		int ret = 0;
		free(pcJsonbody);
		if (ret >= 0)
		{
			code = 200;
		}
		else
		{
			code = 400;
		}
		json2 = cJSON_CreateObject();
		if (json2)
		{
			char uuid[64] = { 0 };

			GF_GetGUID(uuid);
			cJSON_AddStringToObject(json2, "mid", uuid);
			cJSON_AddNumberToObject(json2, "code", code);
			cJSON_AddStringToObject(json2, "replyId", mid);
			cJSON_AddStringToObject(json2, "deviceSn", deviceSn);
			char cTime[128] = { 0 };
			int ret = systemDate2(cTime);
			cJSON_AddStringToObject(json2, "msgTime", cTime);

			char* pcJsonRes = cJSON_PrintUnformatted(json2);
			sprintf(topic, "/v1/devices/device/response", deviceSn);
			ret = IOTA_SendMqtt(topic, pcJsonRes, strlen(pcJsonRes));
			if (ret < 0)
			{
				printf("lowPowerSleepConf sendMsg code %d erro ret[%d]\n",code, ret);
			}
			free(pcJsonRes);

		}
		cJSON_Delete(json2);
		
	}
	return 0;
}
